// packageC/pages/checkDouYin/checkDouYin.js

const app = getApp()
const common = require('../../../public.js')
Page({

    /**
     *   页面的初始数据
     */
    data: {
        checkCode:'',
        checkTopImg:'http://feiyangimage.oss-cn-shanghai.aliyuncs.com/check_top.jpg',
        checkBottomImg:'http://feiyangimage.oss-cn-shanghai.aliyuncs.com/check_bottom.png',
        scanImg:'http://feiyangimage.oss-cn-shanghai.aliyuncs.com/check_.png',
    },
  
 
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        clearInterval(this.data.timer);
        console.log('扫码核销码--',options);
        let screenHeight = wx.getSystemInfoSync().windowHeight
        let okHeight = (screenHeight - 88 - 110 - 80 - 65)
        const that = this
        this.setData({
            toView: 'a1',
        })
        // 返回按钮
        let capsule = wx.getMenuButtonBoundingClientRect()
        this.setData({
            okHeight,
            height: capsule.height,
            top: capsule.top,
         
        })

        if (options.q) {
            let scan_url = decodeURIComponent(options.q);
            console.log("进来0")
  
            let pos1 = scan_url.indexOf("storeId="); // 
            // let pos2 = scan_url.indexOf("&storeName="); // 
            let pos3 = scan_url.indexOf("&platform="); // 

            var storeId = scan_url.substring(pos1+8, pos3)
            // var storeName = scan_url.substring(pos2+11, pos3)
            var platform = scan_url.substring(pos3+10, scan_url.length)
 
            options.storeId = storeId;
            // options.storeName = storeName;
            options.platform = platform;

            console.log('赋值后--storeId--',options.storeId,'---platform',options.platform);
            // console.log('赋值后--storeId--',options.storeId,'---storeName',options.storeName,'---platform',options.platform);
     

        that.setData({
            storeId:options.storeId,
            // storeName:options.storeName,
            platform:options.platform
        })


        // 用户信息
        common.getLocalUserInfo((success, data) => {
            if (success) {
                this.setData({
                    userInfo: data.user,
                    token: data.token,
                   
                    openid: data.openid,
                  
    
                
                    storeName:options.name,
   
                }, () => {
                    console.log('ddd执行了-----');


                    
                })
            }
        }, true)
return;
    }


    that.setData({
        storeId:options.storeId,
        // storeName:options.storeName,
        platform: options.platform,

    },()=>{
        console.log('storeId-',that.data.storeId,'--dd---',that.data.platform);
        // console.log('storeId-',that.data.storeId,'--storeName---',that.data.storeName,'--dd---',that.data.platform);
    })

    common.getLocalUserInfo((success, data) => {
        if (success) {
            this.setData({
                userInfo: data.user,
                token: data.token,
               
                openid: data.openid,
       
                storeName:options.storeName,

            }, () => {
                console.log('执行了-----');

               


                
            })
        }
    }, true)

    },

    checkQrFunc(){
        wx.scanCode({
            success: (res) => {
                const result = res.result
                console.log(result, "1111133333333333333333")
                //1. 扫码支付
                if (result && result.indexOf('/v.douyin.com') != -1) {
                    common.showToast('暂不支持当前功能~')
                    console.log('666');
                    // const quary = result.split('?')[1]
                    // console.log(quary,"1111")
                    // wx.redirectTo({
                    //     url: '/pages/scanForPay/index?' + quary,
                    // })
                } else {
                    common.showToast('暂不支持当前功能~')
                   

                }
            },
            fail: (res) => {
                if (res.errMse != 'scanCode:fail cancel') {
                    common.showToast('扫码失败')
                }
                // wx.redirectTo({
                //     url: '/pages/index/index',
                // })

            }
        })
    },
    inputEvent: function (e) {
       
        console.log('核销码为--',e.detail.value);


        this.setData({ 
            checkCode: e.detail.value
         });
      },   

      checkFunc(){
        if (!this.data.checkCode.length) {
          return  common.showToast('请输入核销码')
        }

        this.checkApi();
      },

      checkApi(){
        let that = this
       
        let tempJson = {
            token: that.data.token,
            userId: that.data.userInfo.id,
            storeId: that.data.storeId,
            checkCode: that.data.checkCode, // 1为手动领取，2为系统群发
     
        }
        common.post("/order/checkDouyinOrder", tempJson, function (res) {
            if (res.status == 200) {
                wx.showToast({
                    title: '核销成功',
                    icon: "none"
                })
                that.setData({
                    checkCode: ''
                }, () => {
                  
                })

            } else {
                // common.showToast('核销码不正确')
                wx.showToast({
                    title: res.msg ? res.msg : '核销失败',
                    icon: "none"
                })
            }
        })
      }
})